var packInfoTable = function() {
	return {
		init: function(pkId, colId) {
			monitorDetailCount = 0;
			$("#form_colClientId").text(colId);
			$("#pkId").val(pkId);
			setPackInfo();
			//获取刷新时间配置
			var cycle;
			$.ajax({
				"type" : "post",
				"url" : "main/sysDictIndex/getDictByParentKey.mvc",
				"async" : false,
				"dataType" : "json",
				"data" : {
					"parentKey" : 'REFRESH_CYCLE'
				},
				"success" : function(resp) {
					if(resp.dict.length!=0){
						cycle = resp.dict[0].dictKey;
					}else {
						cycle = 5000;
					}
				}
			});
			monitorRefreshTimer = setInterval(setPackInfo, cycle);
			
			//设置电池信息
			function setPackInfo () {
				App.blockUI($("#regInfoDiv"));
				var d = {};
				d['colClientId'] = colId;
				var pCode = $("#form_pack_code").val();
				//查询电池详情并赋值
				$.ajax({
					"type" : "post",
					"url" : "main/packRegMng/getRegPackDetail.mvc",
					"dataType" : "json",
					"data" : {
						"colClientId" : colId,
						"packCode" : pCode
					},
					"success" : function(resp) {
						//页面赋值
						var packCur = resp.baseInfo;
						if (packCur) {
							$("span[id^='info_']").each(function(e){
								var id=$(this).attr("id");
								var idStr = id.substr(5);
								$("#" + id).empty();
								eval("$('#" + id + "').append(packCur." +idStr + ")");
							});
						} else {
							$("span[id^='info_']").each(function(e){
								var id=$(this).attr("id");
								$("#" + id).empty();
							});
						}
						createTableList(resp.group);
						App.unblockUI($("#regInfoDiv"));
					},
					error:function(e){
						App.unblockUI($("#regInfoDiv"));
						rmsBox.notify('error', {msg: '出错了！',delay:3000});
					}
				});
			};
			
			/**
			 * 生成电压和温度动态表格
			 * @param group 电压&温度 data
			 * @returns
			 */
			function createTableList(group){
				$("#real_time_info").empty();
				var groupNo = "";
				var type = "";
				var tableId = "";
				var index=0;
				for (var i=0;i<group.length;i++) {
					if (groupNo != group[i].groupNo || type != group[i].type){
						//new table
						index = index + 1;
						groupNo = group[i].groupNo;
						type = group[i].type;
						tableId = "info_table" + index;
						$("#real_time_info").append("<li id='" + tableId + "'></li>");
						if (type == '1') {
							//电压
							$("#" + tableId).append("<p class='c-red c-bold'><span class='fl-left'></span>蓄电池模块号："
									+ groupNo + "&nbsp;电压（V）<span class='fl-right'></span></p><table class='table table-bordered'><tr class='tdHead'></tr><tr class='tdData'></tr></table>");
						} else {
							$("#" + tableId).append("<p class='c-red c-bold'><span class='fl-left'></span>蓄电池模块号："
									+ groupNo + "&nbsp;采样点温度（℃）<span class='fl-right'></span></p><table class='table table-bordered'><tr class='tdHead'></tr><tr class='tdData'></tr></table>");
						}
					}
					if (type == '1'){
						$("#" + tableId + " .tdHead").append("<th>" + group[i].groupCoreNo + "号电压</th>");
						$("#" + tableId + " .tdData").append("<td>" + group[i].data + "</td>");
					} else {
						$("#" + tableId + " .tdHead").append("<th>" + group[i].groupCoreNo + "号采样点温度</th>");
						$("#" + tableId + " .tdData").append("<td>" + group[i].data + "</td>");
					}
				}
			};
			
			/**
			 * 根据输入的电池ID查询关联的客户信息
			 */
			$("#form_pack_code").bind('change',function(){
				var packCode = $(this).val().trim();
				if (packCode == '') {
					$("#form_pack_model").val('');
					$("#form_sim_phoneno").val('');
					$("#form_customId").val('');
					$("#form_cusName").val('');
					return;
				}
				$.ajax({
					"type" : "post",
					"url" : "main/packRegMng/getRegCustomByPackcode.mvc",
					"dataType" : "json",
					"data" : {
						"packCode" : packCode
					},
					"success" : function(resp) {
						var cus = resp.customer;
						if (cus) {
							$("#form_customId").val(cus.pkId);
							$("#form_cusName").val(cus.cusName);
							$("#form_sim_phoneno").val(resp.simPhoneno);
							$("#form_pack_model").val(resp.packModel);
						} else {
							$("#form_customId").val('');
							$("#form_cusName").val('');
							$("#form_sim_phoneno").val('');
							$("#form_pack_model").val('');
							util.showModal("#_alertModal", '该电池ID非法，请检查数据是否正确！');
						}
					}
				});
			});
		}
	}
}();


/**
 * 注册并打印按钮
 */
$("#register").click(function(){
	var pack={};
	var packCode = $("#form_pack_code").val();
	var colClientId = $("#form_colClientId").text();
	var simPhoneno = $("#form_sim_phoneno").val();
	var packModel = $("#form_pack_model").val();
	var customId = $("#form_customId").val();
	var id = $("#pkId").val();
	if (packCode == '') {
		util.showModal("#_alertModal", '请输入电池ID！');
		return;
	} else if(colClientId == '') {
		util.showModal("#_alertModal", '无采集终端ID，请刷新后重试！');
		return;
	} else if (customId == ''|| simPhoneno == '' || packModel == '') {
		util.showModal("#_alertModal", '该电池ID非法，请检查数据是否正确！');
		return;
	}
	pack['packCode'] = packCode;
	pack['colClientId'] = colClientId;
	pack['simPhoneno'] = simPhoneno;
	pack['packModel'] = packModel;
	pack['customId'] = customId;
	pack['pkId'] = id;
	$.ajax({
		"type" : "post",
		"url" : "main/packRegMng/changeClient.mvc",
		"dataType" : "json",
		"data" : pack,
		"success" : function(resp) {
			if(resp.msg=='error') {
				util.alertMsg("提示", "该电池ID终端已变更或电池已报废，请刷新后重试");
				return;
			} else {
				rmsBox.notify('success', {msg: '终端更换成功！'});
				setTimeout(function(e) {
					$('#print_btn').click();
					if ($('#print_btn').attr("href")) {
						$("#print_colClientId").html(colClientId);
						$("#print_packNum").html(packCode);
						$("#print_cusName").html($("#form_cusName").val());
					} else {
						rmsBox.notify('success', {msg: '注册成功！'});
						$('#print_btn').attr("href", "#dayin");
						$('#print_btn').click();
					}
				},1000);
			}
		}
	});
});

$("#print_confirm").click(function(e){
	util.showContent("pages/basic/packRegMng.jsp");
	$("#print_area").jqprint();
});
$("#print_close").click(function(e){
	util.showContent("pages/basic/packRegMng.jsp");
});
